package com.youbang.login.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.druid.util.StringUtils;
import com.youbang.base.annotations.NotCheckSession;
import com.youbang.bsMgr.user.model.UserEntity;
import com.youbang.bsMgr.user.service.UserService;

@Controller
public class LoginController {
    
	@Autowired
	UserService userService;
	
	//系统登陆页面
    @RequestMapping("/")
    @NotCheckSession
    public String index(){
    	return "main/login";
    }
    //首页
    @RequestMapping("/main")
    public String main(){
    	return "main/main";
    }
    
    @RequestMapping("/editDemo")
    public String editDemo(Model model){
    	UserEntity user  = new UserEntity();
    	user.setLoginId("leon");
    	user.setUserName("梁晓飞");
    	user.setPhone("13600000000");
    	user.setEmail("leon@youbang.com");
    	
    	model.addAttribute("user",user);
    	
    	return "bsMgr/user/userEdit";
    }
    
//    @RequestMapping("/listDemo")
//    public String listTable(Model model){
//    	
//    	List<UserEntity> userList = new ArrayList<UserEntity>();
//    	for(int i=0;i<20;i++) {
//    		UserEntity user  = new UserEntity();
//        	user.setLoginId("leon"+i);
//        	user.setUserName("梁晓飞"+i);
//        	user.setPhone("1360000000"+i);
//        	user.setEmail("leon"+i+"@youbang.com");
//        	
//        	userList.add(user);
//    	}
//    	
//    	model.addAttribute("userList",userList);
//    	
//    	return "bsMgr/user/userListPage";
//    }
    
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(HttpServletRequest request, UserEntity user, Model model){
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPasswd())) {
            request.setAttribute("msg", "用户名或密码不能为空！");
            return "login";
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(),user.getPasswd());
        try {
            subject.login(token);
            return "redirect:usersPage";
        }catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
            return "login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("msg", "用户或密码不正确！");
            return "login";
        }
    }
    @RequestMapping("/logOut")
    public String logOut(HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        session.removeAttribute("user");
        return "login";
    }
}